package cn.iocoder.yudao.module.product.job;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.product.dal.dataobject.resourceshare.ResourceShareDO;
import cn.iocoder.yudao.module.product.dal.mysql.resourceshare.ResourceShareMapper;
import cn.iocoder.yudao.module.product.job.luoqi.ResourceShare;
import cn.iocoder.yudao.module.product.job.luoqi.ResourceShareSource;
import cn.iocoder.yudao.module.product.job.luoqi.ResultDeatil;
import cn.iocoder.yudao.module.product.job.luoqi.ResultLuoQI;
import cn.iocoder.yudao.module.product.job.utils.HttpUtils;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时查询共享数据最新资源，并且保存
 */

@Slf4j
@Component
public class ResourceShareJob implements JobHandler {


    @Resource
    private ResourceShareMapper resourceShareMapper;

    @Override
    @TenantJob // 标记多租户
    public String execute(String param) {
        log.info("定时任务开始查询数据资源");
        for (int i = 0; i < Long.parseLong(param); i++) {
            log.info("###【定时任务开始查询数据资源,查询页数为:{}】###",i);
            String url = "https://xcx.you123.top/api/v2/fuyehome/getResourceList?pageNum=" + i;
            String get = HttpUtils.sendGet(url);
            ResultLuoQI<ResourceShareSource> resultLuoQi = JSONObject.parseObject(get, new TypeReference<ResultLuoQI<ResourceShareSource>>() {
            });
            List<ResourceShareSource> data = resultLuoQi.getData();
            if (ObjectUtils.isEmpty(data)) {
                return "数据为空,结束循环";
            }
            log.info("###【捕捉到数据不为空,开始进行保存操作!】###");
            for (ResourceShareSource datum : data) {
                String url1 = "https://xcx.you123.top/api/v2/fuyehome/getDetail?resourceId=" + datum.getResourceId() + "&token=";
                String get1 = HttpUtils.sendGet(url1);
                ResultDeatil resultDeatil = JSONObject.parseObject(get1, new TypeReference<ResultDeatil>() {
                });
                cn.iocoder.yudao.module.product.job.luoqi.ResourceDate resourceDate = resultDeatil.getData();
                ResourceShareSource resource = resourceDate.getResource();
                ResourceShare share = new ResourceShare();
                BeanUtils.copyProperties(resource, share);
                ResourceShareDO resourceShare = cn.iocoder.yudao.framework.common.util.object.BeanUtils.toBean(share, ResourceShareDO.class);
                ResourceShareDO shareDO = resourceShareMapper.selectByResourceId(resourceShare.getResourceId());
                if (ObjectUtils.isEmpty(shareDO)) {
                    log.info("数据为空,开始进行添加,数据为:{}",resourceShare);
                    resourceShareMapper.insert(resourceShare);
                }
            }
        }
        return "success";
    }
}
